﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<script>
    $(function () {
        initControl();
        gridList();
    });
    function gridList() {
        var $gridList = $("#gridList");
        var organizeData;
        $.ajax({
            type: 'get',
            datatype: "json",
            url: "/SystemManage/SysOrganize/GetDictionaryJson",
            success: function (data) {
                organizeData = JSON.parse(data);

                $gridList.dataGrid({
                    url: "/SystemManage/SysUser/GetGridPaginationJson",
                    height: $(window).height() - 128,
                    postData: {
                        companyId: $("#OrganizeId").select2().val(),
                        departmentId: $("#DepartmentId").select2().val(),
                        positionId: $("#PositionId").select2().val(),
                        userId: $("#UserId").select2().val()
                    },
                    colModel: [
                        { label: '主键', name: 'Id', hidden: true },
                         {
                             label: '公司', name: 'OrganizeId', width: 150, align: 'left',
                             formatter: function (cellvalue) {
                                 return organizeData[cellvalue] == null ? "" : organizeData[cellvalue].fullname;
                             }
                         },
                       {
                           label: '部门', name: 'DepartmentId', width: 80, align: 'left',
                           formatter: function (cellvalue) {
                               return organizeData[cellvalue] == null ? "" : organizeData[cellvalue].fullname;
                           }
                       },
                      {
                          label: '岗位', name: 'PositionId', width: 80, align: 'left',
                          formatter: function (cellvalue) {
                              return organizeData[cellvalue] == null ? "" : organizeData[cellvalue].fullname;
                          }
                      },
                        { label: '账户', name: 'Account', width: 100, align: 'left' },
                        { label: '姓名', name: 'RealName', width: 100, align: 'left' },
                        {
                            label: '性别', name: 'Gender', width: 60, align: 'center',
                            formatter: function (cellvalue) {
                                if (cellvalue === true) {
                                    return '男';
                                } else {
                                    return '女';
                                }
                            }
                        },
                        //{ label: '手机', name: 'MobilePhone', width: 80, align: 'left' },
                        {
                            label: '创建时间', name: 'CreatorTime', width: 80, align: 'left',
                            formatter: "date", formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' }
                        },
                        {
                            label: "允许登录", name: "EnabledMark", width: 60, align: "center",
                            formatter: function (cellvalue) {
                                if (cellvalue === true) {
                                    return '<span class=\"label label-success\">正常</span>';
                                } else {
                                    return '<span class=\"label label-default\">禁用</span>';
                                }
                            }
                        },
                        { label: '备注', name: 'Description', width: 200, align: 'left' }
                    ],
                    pager: "#gridPager",
                    sortname: 'CreatorTime desc',
                    viewrecords: true
                });
                $("#btn_search").click(function () {
                    $gridList.jqGrid('setGridParam', {
                        postData: {
                            companyId: $("#OrganizeId").select2().val(),
                            departmentId: $("#DepartmentId").select2().val(),
                            positionId: $("#PositionId").select2().val(),
                            userId: $("#UserId").select2().val()
                        }
                    }).trigger('reloadGrid');
                });
            }

        });
    }

    function initControl() {

        $("#Gender").bindSelect();
        $("#IsAdministrator").bindSelect();
        $("#EnabledMark").bindSelect();
        $("#OrganizeId").bindSelect({
            url: "/SystemManage/SysOrganize/GetCompanyTreeSelectJson"
        });

        //alert($("#OrganizeId").val()); //获取选中值
        //$("#OrganizeId").val("cff39c04-b4fa-407b-8baa-1ca140a833dc").trigger("change"); //默认选中
        $('#OrganizeId').change(function () {
            //  alert($(this).val());
            $("#DepartmentId").empty();
            $("#DepartmentId").append("<option value=''>请选择部门</option>");
            $("#DepartmentId").bindSelect({
                url: "/SystemManage/SysOrganize/GetDepartmentTreeSelectJson?parentId=" + $(this).val()
            });

            $("#DepartmentId").prop("disabled", false);
        });

        $('#DepartmentId').change(function () {
            $("#PositionId").empty();
            $("#PositionId").append("<option value=''>请选择职位</option>");
            $("#PositionId").bindSelect({
                url: "/SystemManage/SysOrganize/GetWorkTreeSelectJson?parentId=" + $(this).val()
            });
            $("#PositionId").prop("disabled", false);
        });

        //部门禁用
        $("#DepartmentId").prop("disabled", true);
        $("#PositionId").prop("disabled", true);

        $.ajax({
            url: "/SystemManage/SysUser/GetTreeSelectBusinessJson",
            dataType: "json",
            async: false,
            success: function (data) {
                $("#UserId").select2({
                    data: data,
                    placeholder: '请选择用户'
                });
            }
        });

    }

    function btn_add() {
        $.modalOpen({
            id: "Form",
            title: "新增用户",
            url: "/SystemManage/SysUser/Form",
            width: "700px",
            height: "510px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
            }
        });
    }
    function btn_edit() {
        var keyValue = $("#gridList").jqGridRowValue().Id;
        $.modalOpen({
            id: "Form",
            title: "修改用户",
            url: "/SystemManage/SysUser/Form?keyValue=" + keyValue,
            width: "700px",
            height: "510px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
            }
        });
    }
    function btn_delete() {
        $.deleteForm({
            url: "/SystemManage/SysUser/DeleteForm",
            param: { keyValue: $("#gridList").jqGridRowValue().Id },
            success: function () {
                $.currentWindow().$("#gridList").trigger("reloadGrid");
            }
        });
    }

    function btn_revisepassword() {
        var keyValue = $("#gridList").jqGridRowValue().Id;
        var account = $("#gridList").jqGridRowValue().Account;
        var realName = $("#gridList").jqGridRowValue().RealName;
        $.modalOpen({
            id: "RevisePassword",
            title: '重置密码',
            url: '/SystemManage/SysUser/RevisePassword?keyValue=' + keyValue + "&account=" + escape(account) + '&realName=' + escape(realName),
            width: "450px",
            height: "260px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
            }
        });
    }

</script>

<div class="topPanel">
    <div class="toolbar">
        <div class="btn-group">
            <a id="NF-add" authorize="yes" class="btn btn-primary dropdown-text" onclick="btn_add()"><i class="fa fa-plus"></i>新建用户</a>
        </div>
        <div class="operate">
            <ul class="nav nav-pills">
                <li class="first">已选中<span>1</span>项</li>
                <li><a id="NF-edit" authorize="yes" onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>修改用户</a></li>
                <li><a id="NF-delete" authorize="yes" onclick="btn_delete()"><i class="fa fa-trash-o"></i>删除用户</a></li>
                <li class="split"></li>
                <li><a id="NF-revisepassword" authorize="yes" onclick="btn_revisepassword()"><i class="fa fa-key"></i>密码重置</a></li>

            </ul>
            <a href="javascript:;" class="close"></a>
        </div>
        <script>$('.toolbar').authorizeButton()</script>
    </div>
    <div class="search">
        <table>
            <tr>
                <td>
                    <div class="input-group">
                        <select id="OrganizeId" name="OrganizeId" class="form-control" placeholder="请选择公司">
                            <option value="">请选择公司</option>
                        </select>
                    </div>
                </td>

                <td style="padding-left: 10px;">
                    <div class="input-group">
                        <select id="DepartmentId" name="DepartmentId" class="form-control" placeholder="请选择部门">
                            <option value="">请选择部门</option>
                        </select>
                    </div>
                </td>

                <td style="padding-left: 10px;">
                    <div class="input-group">
                        <select id="PositionId" name="PositionId" class="form-control" placeholder="请选择职位">
                            <option value="">请选择职位</option>
                        </select>
                    </div>
                </td>
                <td style="padding-left: 10px;">
                    <div class="input-group">
                        <select id="UserId" name="UserId" class="form-control" placeholder="请选择用户">
                            <option value="">请选择用户</option>
                        </select>
                    </div>
                </td>
                <td style="padding-left: 10px;">
                    <div class="input-group">
                        <span class="input-group-btn">
                            <button id="btn_search" type="button" class="btn  btn-primary"><i class="fa fa-search"></i></button>
                        </span>
                    </div>
                </td>

            </tr>
        </table>
    </div>
</div>
<div class="gridPanel">
    <table id="gridList"></table>
    <div id="gridPager"></div>
</div>
